﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using TestSystem.DataManipulation;
using TestSystem.Ui.Helpers;
using Quote = TestSystem.Model.Data.Quote;

namespace TestSystem.Ui.Forms
{
    public partial class BarForm : Form
    {
        private DateTime datetime;
        private Decimal high;
        private Decimal open;
        private Decimal low;
        private Decimal close;
        private int volume;
        public Model.Data.Quote Quote { get; private set; }
        public FormType FormType { private get; set; }
       
        public BarForm()
        {
            InitializeComponent();
            LowValue.Validating += LowValueValidating;
            HighValue.Validating += HighValueValidating;
            DateTimePicker.Tag = true;
            OpenValue.Tag = false;
            HighValue.Tag = false;
            LowValue.Tag = false;
            CloseValue.Tag = false;
            VolumeValue.Tag = false;
        }
        #region Validating

        private void VolumeValueValidating(object sender, CancelEventArgs e)
        {
            var digitbox = (TextBox)sender;
            SetValues();
            Int32 currentvalue;
            if ((digitbox.Text != string.Empty) && (Int32.TryParse(digitbox.Text, NumberStyles.Any,
                 CultureInfo.GetCultureInfo(("en-US")), out currentvalue)))
                digitbox.Tag = currentvalue > 0;
            else digitbox.Tag = false;
        }

        private void DateTimePickerValidating(object sender, CancelEventArgs e)
        {
            DateTimePicker.Tag = DateTimePicker.Value <= DateTime.Now;
        }

        private void LowValueValidating(object sender, CancelEventArgs e)
        {
            if (!CheckNullValues()) return;
            var minvalue = new[] { open, high, low, close }.Min();
            LowValue.Tag = low == minvalue;
        }

        private void HighValueValidating(object sender, CancelEventArgs e)
        {
            if (!CheckNullValues()) return;
            var maxvalue = new[] { open, high, low, close }.Max();
            HighValue.Tag = high == maxvalue;
        }
        private void OnValidating(object sender, CancelEventArgs e)
        {
            var digitbox = (TextBox)sender;
            SetValues();
            Decimal currentvalue;
            if ((digitbox.Text != string.Empty) && (Decimal.TryParse(digitbox.Text, NumberStyles.Any,
                 CultureInfo.InvariantCulture, out currentvalue)))
                digitbox.Tag = currentvalue > 0;
            else digitbox.Tag = false;
        }

        

        private void OnValidated(object sender, EventArgs e)
        {
            OkBtn.Enabled = AreAllValuesCorrect();
        }
        private bool AreAllValuesCorrect()
        {
            return (Boolean)OpenValue.Tag && (Boolean)HighValue.Tag && (Boolean)LowValue.Tag && (Boolean)CloseValue.Tag &&
                   (Boolean)VolumeValue.Tag && (Boolean)DateTimePicker.Tag;
        }
        #endregion

        private void OkBtnClick(object sender, EventArgs e)
        {

            datetime = DateTimePicker.Value;
            Quote = new Quote
                        {
                            DateTime = datetime,
                            Open = open,
                            High = high,
                            Low = low,
                            Close = close,
                            Volume = volume,
                            
                            
                        };
            DialogResult = DialogResult.OK;
             
            Close();
        }

        private Boolean CheckNullValues()
        {
            return open != 0 && high != 0 && low != 0 && close != 0;
        }

        private void SetValues()
        {

            Decimal.TryParse(OpenValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                             out open);
            Decimal.TryParse(HighValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                             out high);
            Decimal.TryParse(LowValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                             out low);
            Decimal.TryParse(CloseValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                             out close);
            Int32.TryParse(VolumeValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                             out volume);

        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BarFormLoad(object sender, EventArgs e)
        {
            
            DateTimePicker.CustomFormat = @"MM.dd.yyyy HH:mm:ss";
            Text = @"Bar";
            SetValues();
            SetDecimalSeparator();
            if (FormType == FormType.TypeAdd)
            {
                OpenValue.Text = MessagingHelper.BeginValues;
                HighValue.Text = MessagingHelper.BeginValues;
                LowValue.Text = MessagingHelper.BeginValues;
                CloseValue.Text = MessagingHelper.BeginValues;
                OkBtn.Enabled = false;
            }
            else
            {
                OpenValue.Tag = true;
                HighValue.Tag = true;
                LowValue.Tag = true;
                CloseValue.Tag = true;
                VolumeValue.Tag = true;
            }
        }

        private void SetDecimalSeparator()
        {
            OpenValue.Text = OpenValue.Text.Replace(',', DecimalHelper.GetDecimalSeparator());
            OpenValue.Text = OpenValue.Text.Replace('.', DecimalHelper.GetDecimalSeparator());
            HighValue.Text = HighValue.Text.Replace(',', DecimalHelper.GetDecimalSeparator());
            HighValue.Text = HighValue.Text.Replace('.', DecimalHelper.GetDecimalSeparator());
            LowValue.Text = LowValue.Text.Replace(',', DecimalHelper.GetDecimalSeparator());
            LowValue.Text = LowValue.Text.Replace('.', DecimalHelper.GetDecimalSeparator());
            CloseValue.Text =  CloseValue.Text.Replace(',', DecimalHelper.GetDecimalSeparator());
            CloseValue.Text = CloseValue.Text.Replace('.', DecimalHelper.GetDecimalSeparator());

        }

        private void TextBoxTextChanged(object sender, EventArgs e)
        {
            var digitbox = (TextBox) sender;
            digitbox.Text = digitbox.Text.Replace(',', DecimalHelper.GetDecimalSeparator());
            digitbox.Text = digitbox.Text.Replace('.', DecimalHelper.GetDecimalSeparator());
            digitbox.SelectionStart = digitbox.Text.Length;
        }

    }
}
